const a = async function (
  _,
  { origin, me, memory },
  { dc: { http, store, send, update }, xe: { get, set }, dayjs }
) {
  const form = get(origin, "a4b7815ba2e74531a4c84d3a34709817", {});
  const YEAR = dayjs(store.get("user", "date")).format("YYYY");
  const params = { ...form };
  Object.keys(params).forEach((key) => {
    const value = params[key];
    if (value && Array.isArray(value)) {
      params[key] = value.join();
    }
  });
  const sxHead = [
    {
      order: 10,
      id: "1",
      field: "PNAME",
      title: "部门名称",
      width: { minWidth: 220 },
    },
    {
      order: 20,
      id: "2",
      field: "ENAME",
      title: "单位名称",
      width: { minWidth: 220 },
    },
    {
      order: 30,
      id: "3",
      field: "NAME",
      title: "债务名称",
      width: { minWidth: 180 },
    },
    {
      order: 40,
      id: "4",
      field: "DEBTTYPENAME",
      title: "债务类别",
      width: { minWidth: 140 },
    },
    {
      order: 50,
      id: "5",
      field: "LOANTYPENAME",
      title: "债权类型",
      width: { minWidth: 140 },
    },
    {
      order: 60,
      id: "6",
      field: "BORROWTYPENAME",
      title: "增信措施",
      width: { minWidth: 140 },
    },
    {
      order: 70,
      id: "7",
      field: "LOANNAME",
      title: "债权人",
      width: { minWidth: 140 },
    },
    {
      order: 80,
      id: "8",
      field: "LOANERNAME",
      title: "债权人全称",
      width: { minWidth: 160 },
    },
    {
      order: 90,
      id: "9",
      field: "REMARK",
      title: "备注",
      width: { minWidth: 160 },
    },
    {
      order: 100,
      id: "10",
      field: "ITEMNAME",
      title: "项目名称",
      width: { minWidth: 180 },
    },
    {
      order: 110,
      id: "11",
      field: "ITEMTYPENAME",
      title: "项目分类",
      width: { minWidth: 140 },
    },
    {
      order: 120,
      id: "12",
      field: "BOOKDATE",
      title: "签订日期",
      width: { minWidth: 100 },
    },
    {
      order: 130,
      id: "13",
      field: "AGREEMONEY",
      title: "协议金额",
      ui: {
        self: { align: "right", minWidth: 130, formatter: ["money", 1] },
        custom: { summary: "sum" },
      },
    },
    {
      order: 140,
      id: "14",
      field: "RATETYPENAME",
      title: "利率类型",
      width: { minWidth: 100 },
    },
    {
      order: 150,
      id: "15",
      field: "YEARRATE",
      title: "年利率/%",
      width: { minWidth: 100 },
    },
    {
      order: 160,
      id: "16",
      field: "YEARCOST",
      title: "年费率/%",
      width: { minWidth: 100 },
    },
    {
      order: 170,
      id: "17",
      field: "AGREENO",
      title: "协议号",
      width: { minWidth: 120 },
    },
    {
      order: 180,
      id: "18",
      field: "ZWQX",
      title: "债务期限/月",
      width: { minWidth: 120 },
    },
    {
      order: 190,
      id: "19",
      field: "ISRECESSIVE",
      title: "是否隐性债务",
      width: { minWidth: 120 },
    },
  ];
  const year = Number(YEAR);
  const szHead = [
    { label: "期初债务余额", value: "M1" },
    { label: "本期举借数", value: "M5" },
    { label: "本期偿还数", value: "M2" },
    { label: "期末债务余额", value: "M4" },
    { label: "支付利息", value: "M3" },
    { label: "支出信息", value: "M7" },
    { label: "隐性债务期初金额", value: "M48" },
    { label: "隐性债务举借金额", value: "M50" },
    { label: "隐性债务偿还金额", value: "M51" },
    { label: "隐性债务期末金额", value: "M49" },
    { t_no: 0, label: "年消债计划", value: "M52" },
    { t_no: 1, label: "年消债计划", value: "M53" },
    { t_no: 2, label: "年消债计划", value: "M54" },
    { t_no: 3, label: "年消债计划", value: "M55" },
    { t_no: 4, label: "年消债计划", value: "M56" },
    { t_no: 0, label: "年前应还债务", value: "M8" },
    { t_no: 1, label: "年应还债务", value: "M9" },
    { t_no: 2, label: "年应还债务", value: "M10" },
    { t_no: 3, label: "年应还债务", value: "M11" },
    { t_no: 4, label: "年应还债务", value: "M12" },
    { t_no: 5, label: "年应还债务", value: "M13" },
    { t_no: 6, label: "年应还债务", value: "M14" },
    { t_no: 7, label: "年应还债务", value: "M15" },
    { t_no: 8, label: "年应还债务", value: "M16" },
    { t_no: 9, label: "年应还债务", value: "M17" },
  ];
  const headObj = {};
  sxHead.forEach((item) => {
    item.align = item.align ?? 0;
    headObj[item.field] = item;
  });
  szHead.forEach((item, index) => {
    headObj[item.value] = {
      order: index * 10 + 400,
      id: index * 10 + 400,
      field: item.value,
      title: `${typeof item.t_no === "number" ? year + item.t_no : ""}${
        item.label
      }`,
      ui: {
        self: { align: "right", minWidth: 140, formatter: ["money", 1] },
        custom: { summary: "sum" },
      },
    };
  });
  const gridConfig = { stripe: true, treeConfig: { transform: false } };
  const { PROPFIELD, NUMFIELD, FROZEN } = form;
  const head = [];
  if (FROZEN) {
    params.FROZEN = undefined;
    const findField = PROPFIELD.find((i) => i === FROZEN);
    if (!findField) {
      PROPFIELD.push(FROZEN);
      params.PROPFIELD += `,${FROZEN}`;
    }
    gridConfig.stripe = false;
    gridConfig.treeConfig = { expandAll: true, transform: false };
    head.push({
      order: 1,
      id: "eq_1",
      ui: {
        self: {
          title: "序号",
          align: "center",
          type: "seq",
          width: 90,
          treeNode: true,
        },
      },
    });
  }
  [...PROPFIELD, ...NUMFIELD].forEach((field) => {
    head.push(headObj[field]);
  });
  head.sort((a, b) => a.order - b.order);
  send({ path: me, action: "head-change", value: head });
  await update({ path: me, action: "attr", value: gridConfig });
  const requestParams = {
    ISRECESSIVE: "-1",
    ...params,
    MONEY: params.MONEY ? "1" : "0",
    id: 2200,
    pageIndex: 0,
    pageSize: 10,
    YEAR,
  };
  const { items } = await http.form({
    url: "/zw/czQuery/listDebtQuery",
    params: requestParams,
  });
  const dataToTal = await http.form({
    url: "/zw/czQuery/listDebtQuerySum",
    params: requestParams,
  });
  set(memory, `${me}.dataToTal`, dataToTal);
  const numList = szHead.map((i) => i.value);
  if (FROZEN) {
    const addRow = {};
    const addNum = function (newObj, oldObj) {
      const obj = { ...oldObj };
      Object.keys(newObj).forEach((key) => {
        if (numList.includes(key)) {
          obj[key] =
            typeof obj[key] === "undefined"
              ? newObj[key]
              : obj[key] + Number(newObj[key]);
        }
      });
      return obj;
    };
    items.forEach((item) => {
      const field = item[FROZEN] ?? " ";
      addRow[field] = addNum(item, addRow[field] ?? {});
      if (!addRow[field].children) {
        addRow[field].children = [];
      }
      addRow[field].children.push(item);
    });
    const treeData = [];
    Object.keys(addRow).forEach((key) => {
      treeData.push({ [FROZEN]: key, ...addRow[key] });
    });
    return { items: treeData, total: 0 };
  }
  return { items, total: 0 };
};












const col1 = [
  {
    label: "部门名称",
    value: "PNAME",
  },
  {
    label: "单位名称",
    value: "ENAME",
  },
  {
    label: "债务名称",
    value: "NAME",
  },
  {
    label: "债务类别",
    value: "DEBTTYPENAME",
  },
  {
    label: "债权类型",
    value: "LOANTYPENAME",
  },
  {
    label: "增信措施",
    value: "BORROWTYPENAME",
  },
  {
    label: "债权人",
    value: "LOANNAME",
  },
  {
    label: "债权人全称",
    value: "LOANERNAME",
  },
  {
    label: "备注",
    value: "REMARK",
  },
  {
    label: "项目名称",
    value: "ITEMNAME",
  },
  {
    label: "项目分类",
    value: "ITEMTYPENAME",
  },
  {
    label: "签订日期",
    value: "BOOKDATE",
  },
  {
    label: "协议金额",
    value: "AGREEMONEY",
  },
  {
    label: "利率类型",
    value: "RATETYPENAME",
  },
  {
    label: "年利率/%",
    value: "YEARRATE",
  },
  {
    label: "年费率/%",
    value: "YEARCOST",
  },
  {
    label: "协议号",
    value: "AGREENO",
  },
  {
    label: "债务期限/月",
    value: "ZWQX",
  },
  {
    label: "是否隐性债务",
    value: "ISRECESSIVE",
  },
];


